package com.uubx.controller;

import com.uubx.pojo.Department;
import com.uubx.pojo.User;
import com.uubx.result.MessageCode;
import com.uubx.result.Result;
import com.uubx.result.ResultUtils;
import com.uubx.service.DepartmentService;
import com.uubx.service.UserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wuguojun
 * @Date 2020/12/26 18:51
 * @Version 1.0
 */
@Controller
@ResponseBody
@RequestMapping("/department")
@Api(description ="部门相关的接口文档")
public class DepartmentController {


    @Autowired
    private DepartmentService departmentService;




    /**
     * 分页查询所有的部门
     * @param page
     * @param limit
     * @return
     */

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value = "分页查询所有的部门",notes = "分页查询所有的部门",httpMethod = "GET",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "当前页数",required=true,paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "分页大小",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryAllUserList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                       @RequestParam(value = "limit",defaultValue = "5")Integer limit){
        if (page<=0){
            return ResultUtils.buildFail(MessageCode.PAGE_ERROR);
        }

        List<Department> departments = departmentService.queryAllDepartment((page - 1) * limit, limit);
        Long count = departmentService.queryAllDepartmentCount();
        return ResultUtils.buildSuccess(departments,count);
    }


    /**
     * 通过部门名称查询部门
     * @param name
     * @return
     */
    @RequestMapping(value = "/queryDepartmentByName",method = RequestMethod.GET)
    @ApiOperation(value = "通过部门名称查询部门",notes = "通过部门名称查询部门",httpMethod = "GET")
    @ApiImplicitParam(name = "name",value = "部门名称",required=true,paramType = "query")
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryDepartmentByName(@RequestParam(value = "name")String name){
        if (name==null||name.equals("")){
            return ResultUtils.buildFail(MessageCode.DEPARTMENT_NAME_EMPTY);
        }
        List<Department> departments = departmentService.queryDepartmentByName(name);
        if (departments==null){
            return ResultUtils.buildFail(MessageCode.SELECT_EMPTY);
        }
        Long count = departmentService.queryDepartmentNameToCount(name);
        return ResultUtils.buildSuccess(departments,count);
    }

    /**
     * 通过启用状态查询部门
     * @param status
     * @return
     */
    @RequestMapping(value = "/queryDepartmentByStatus",method = RequestMethod.GET)
    @ApiOperation(value = "通过启用状态查询部门",notes = "通过启用状态查询部门",httpMethod = "GET")
    @ApiImplicitParam(name = "status",value = "启用状态",required=true,paramType = "query")
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryDepartmentByStatus(@RequestParam(value = "status")Integer status){
        List<Department> departments = departmentService.queryDepartmentByStatus(status);
        Long count = departmentService.queryDepartmentByStatusToCount(status);
        return ResultUtils.buildSuccess(departments,count);
    }


    /**
     * 通过部门id查询到该部门下所有人员
     * @param
     * @return
     */
    @RequestMapping(value = "/queryAllUserByDepartmentId",method = RequestMethod.GET)
    @ApiOperation(value = "通过部门id查询到该部门下所有人员",notes = "通过部门id查询到该部门下所有人员",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryAllUserByDepartmentId(@RequestParam(value = "id")Integer id){
        List<User> users = departmentService.queryAllUsersByDepartmentId(id);
        Long count = departmentService.queryAllUsersByDepartmentIdToCount(id);
        return ResultUtils.buildSuccess(users,count);
    }


    /**
     * 停用部门的状态
     * @param
     * @return
     */
    @RequestMapping(value = "/updateDepartmentStatus",method = RequestMethod.POST)
    @ApiOperation(value = "停用部门的状态",notes = "停用部门的状态",httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result updateUserByStatus(@RequestBody Department department){

        Long count = departmentService.queryAllUsersByDepartmentIdToCount(department.getId());

        if (count!=0){
            return ResultUtils.buildFail(MessageCode.DEPARTMENT_NOT_EMPTY);
        }

        departmentService.updateDepartmentById(department);
        return ResultUtils.buildSuccess();
    }

    /**
     * 删除部门
     * @param
     * @return
     */

    @RequestMapping(value = "/deleteDepartment",method = RequestMethod.GET)
    @ApiOperation(value = "删除部门",notes = "删除部门",httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "部门id",required=true,paramType = "query")})
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result deleteDepartment(@RequestParam(value = "id")Integer id){
        Long count = departmentService.queryAllUsersByDepartmentIdToCount(id);
        if (count!=0){
            return ResultUtils.buildFail(MessageCode.DEPARTMENT_NOT_EMPTY);
        }
        departmentService.deleteDepartmentById(id);
        return ResultUtils.buildSuccess();
    }

    /**
     * 添加部门
     * @param
     * @return
     */

    @RequestMapping(value = "/addDepartment",method = RequestMethod.POST)
    @ApiOperation(value = "添加部门",notes = "添加部门",httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result deleteDepartment(@RequestBody Department department){
       departmentService.addDepartment(department);
        return ResultUtils.buildSuccess();
    }


    /**
     * 查询所有的部门名称
     * @param
     * @return
     */
    @RequestMapping(value = "/queryAllDepartmentName",method = RequestMethod.GET)
    @ApiOperation(value = "查询所有的部门名称",notes = "查询所有的部门名称",httpMethod = "GET",
            authorizations = {@Authorization(value = "token")})//请求需要token
    @ApiResponses({@ApiResponse(code = 200,message = "请求成功！data代表返回的数据"),
            @ApiResponse(code=500,message = "服务器开差了")})
    public Result queryAllRole(){
        List list = departmentService.queryAllDepartmentName();
        Long count = departmentService.queryAllDepartmentCount();
        return ResultUtils.buildSuccess(list,count);
    }








}
